//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Client.Presenters
{
    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using ServiceModel.Client;
    using ServiceModel.Client.Storage;
    using ServiceModel.Contract;
    using Views;

    public class MainWindowPresenter
    {
        private readonly IMainWindow view;
        private readonly IEndpointDescriptionRepository descriptionRepository;
        private readonly IPreflightServiceClientFactory clientFactory;

        public MainWindowPresenter(IMainWindow view, IEndpointDescriptionRepository descriptionRepository, IPreflightServiceClientFactory clientFactory)
        {
            this.view = view;
            this.descriptionRepository = descriptionRepository;
            this.clientFactory = clientFactory;
        }

        public void Initialize()
        {
            try
            {
                var endpoints = this.descriptionRepository.GetEndpoints();
                this.view.SetEndpoints(endpoints.ToList());
            }
            catch (Exception exception)
            {
                var message = MessageBuilder.EndpointsLoadError(exception);
                this.view.ShowError(message);
            }
        }

        public void StartPreflightCheck()
        {
            this.view.SetWaitMode("Running Preflight Checks...");
            Task.Factory
                .StartNew<PreflightCheck>(this.RunPreflightCheck)
                .ContinueWith(task =>
                    {
                        this.view.TurnOffWaitMode();
                        if (task.IsFaulted)
                        {
                            var errorMessage = MessageBuilder.PreflightError(task.Exception.InnerException);
                            this.view.ShowError(errorMessage);
                            return;
                        }

                        this.view.DisplayResults(task.Result);
                    });
        }

        private PreflightCheck RunPreflightCheck()
        {
            var client = this.clientFactory.CreateClient(this.view.SelectedEndpoint);
            if (client == null)
            {
                throw new InvalidOperationException("Cannot find a suitable client for endpoint of type: " + this.view.SelectedEndpoint.Type);
            }

            return client.RunPreflightCheck();
        }
    }
}